
--
-- Author: shunguo.chen
-- Date: 2016-06-28 12:31:51
--
-- MailVo

local Item = require("app.public.dao.Item")
local Equip = require("app.public.dao.Equip")
local Jewel = require("app.public.dao.Jewel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

-- message Mail {
--	 string uid                         = 1; //邮件唯一ID
--	 int32 status                       = 2; //邮件状态(0-新邮件 1-已读 2-附件已领取)
--	 string sender                      = 3; //发送者ID
--	 int32 senderId                     = 4; //发送者名字
--	 string subject                     = 5; //邮件标题
--	 string body                        = 6; //邮件内容
--	 bool canDelete                     = 7; //邮件能否删除
--	 int64 createdTime                  = 8; //邮件创建时间
--	 int64 expiredTime                  = 9; //邮件过期时间
--	 repeated string subject_params     = 10;//邮件标题参数
--	 repeated string body_params        = 11;//邮件内容参数
--	 int32 templateID                   = 12;//邮件模板ID(mail表的ID)
--	 repeated shared.ItemCell itemCells = 13;//邮件附件1(长度为0则没有附件)
--	 repeated pb_loot.Loot loots        = 14;//邮件附件2(长度为0则没有附件)
--	 bool hasAttachment                 = 15;//是否有附件
-- }
function ClassRef:ctor(data)
	if data then
		self:initWithData(data)
	end
end

function ClassRef:initWithData(data)
	-- dump(data,"Mail:init(data)")

	self.uid = data.uid
	self.status = data.status
	self.sender = data.sender
	if self.sender == nil or self.sender == "" then
		self.sender = L("text_11400006")
	end
	self.senderId = data.senderId
	self.subject = data.subject
	self.body = data.body
	self.canDelete = data.canDelete
	self.createdTime = data.createdTime
	self.expiredTime = data.expiredTime

	self.createdTimeStr = os.date("%Y.%m.%d", self.createdTime)

	self.hasAttachment = data.hasAttachment

	self._isGetDetail = false

	self.rewards = {}
	for i,v in ipairs(data.itemCells or {}) do
		local item
		if v.item then
			item = Item.new(Enums.ItemBagType.item)
			item:read(v.item)
		end
		if v.equip then
			item = Equip.new()
			item:read(v.equip)
		end
		if v.jewel then
			item = Jewel.new()
			item:read(v.jewel)
		end
		self.rewards[#self.rewards + 1] = item
	end

	local hasPresentCoin = false
	for i,loot in ipairs(data.loots or {}) do
		if not hasPresentCoin and loot.type == "presentCoin" then
			hasPresentCoin = true
		end
		self.rewards[#self.rewards + 1] = UD:parseLootToBagItem(loot)
	end
	self._hasPresentCoin = hasPresentCoin


	if data.templateID and data.templateID > 0 then
		local conf = GD:queryMailById(data.templateID)
		if conf then
			self.templateID = data.templateID
			self.subject_templateID = conf.title
			self.subject_params = data.subject_params or {}
			self.body_templateID = conf.body
			self.body_params = data.body_params or {}
			self.autoDelete = conf.autoDelete
		end
	else
		--没有模版的内容需要进行urldecode
		self.body = string.urldecode(self.body)
	end

	self.subjectStr = self:parseSubject() or ""
	self.bodyStr = string.nl2br(self:parseBody() or "")

	-- print("Mail.subjectStr = ", self.subjectStr)
	-- print("Mail.bodyStr = ", self.bodyStr)
end

function ClassRef:getSubjectBriefName()
	local name = L(self.subjectStr)
	local len = string.ulen(name)
	if len > Constant.MAIL_SUBJECT_NAME_BRIEF_MAX_LEN then
		return string.usub(name, 1, Constant.MAIL_SUBJECT_NAME_BRIEF_MAX_LEN-1) .. "..."
	else
		return name
	end
end

function ClassRef:setGetDetail(b)
	self._isGetDetail = b
end

function ClassRef:isGetDetail()
	return self._isGetDetail
end

function ClassRef:hasPresentCoin()
	return self._hasPresentCoin
end

function ClassRef:isAutoDelete()
	return self.autoDelete == "1"
end

function ClassRef:isNew()
	return self.status == Enums.MailStatus.new or self.status == nil
end

function ClassRef:canGetItems()
	return self.hasAttachment
end

function ClassRef:canDeleted()
	if self.hasAttachment then
		return false
	else
		return self.status ~= Enums.MailStatus.new
	end
end

function ClassRef:getLeftDays()
	local serverTime = app.session:getServerTime() or os.time()
	local day = 0
	if serverTime < self.expiredTime then
		local sub = self.expiredTime - serverTime
		day = math.ceil(sub / (3600*24))
	end
	return day
end

-- 是否过期
function ClassRef:isExpired()
	local  serverTime= app.session:getServerTime() or os.time()
	return serverTime >= self.expiredTime, self.expiredTime - serverTime
end

function ClassRef:getOverTimeStr()
	-- TODO 优化取时间
	local serverTime = app.session:getServerTime() or os.time()
	if serverTime < self.expiredTime then
		local sub = self.expiredTime - serverTime
		if sub > 3600*24 then
			local day = math.ceil(sub / (3600*24))
			return string.format(L("text_11400002"), day)
		else
			local hour = math.ceil(sub / 3600)
			return string.format(L("text_11400003"), hour)
		end
	else
		return L("tips_1150007")
	end
end

function ClassRef:parseSubject()
	if self.subject_templateID == 0 or self.subject_templateID == nil then
		return self.subject
	end

	local subject = L(self.subject_templateID)
	-- print("pre subject:",subject)
	if #self.subject_params > 0 then
		for k,v in ipairs(self.subject_params) do
			subject = string.format(subject, L(v))
		end
	end
	-- print("after subject:",subject)
	return subject
end

function ClassRef:parseBody()
	if self.body_templateID == 0 or self.body_templateID == nil then
		return self.body
	end

	local body=L(self.body_templateID)--L("mailbody_"..self.body_templateID)
	for k,v in ipairs(self.body_params) do
		self.body_params[k] = L(v)
	end

	-- print("pre body:", body)
	if #self.body_params > 0 then
		-- if self.templateID == 90006 then
		-- 	local c = RankingManager.hcfg[checknumber(self.body_params[1])]
		-- 	if c then
		-- 		self.body_params[1] = L(c.name)
        --     end
        --     body = string.format(body, unpack(self.body_params))
		-- else
		if self.templateID == 90021 or self.templateID == 90022 then
            --需要读取道具表或者装备表获知是什么道具装备名
            --第一个参数是类型 第二是id
            local item = UD:parseRewardToBagItem(self.body_params[1].."|"..self.body_params[2].."|1")
            if item then
                body = string.format(body, item:getName(true))
            end
        else
            body = string.format(body, unpack(self.body_params))
		end
	end
	-- print("after body:", body)
	return body
end

return ClassRef
